#pragma once

#include <iostream>
#include <unistd.h>
#include <sys/poll.h>
#include "Socket.hpp"
#include "Log.hpp"

using namespace SocketModule;
using namespace LogModule;

class PollServer
{
    static const int size = 4096;
    static const int defaultfd = -1;

public:
    PollServer(int port)
        : _listensock(std::make_unique<TcpSocket>()),
          _isrunning(false)
    {
        _listensock->BuildListenSocketMethod(port);
        for (int i = 0; i < size; i++)
        {
            _fds[i].fd = defaultfd;
            _fds[i].events = 0;
            _fds[i].revents = 0;
        }

        _fds[0].fd = _listensock->Fd();
        _fds[0].events = POLLIN;
    }

    void Start()
    {
        int timeout = 1000;
        _isrunning = true;
        while (_isrunning)
        {

            int n = poll(_fds, size, timeout);
            switch (n)
            {
            case -1:
                LOG(LogLevel::ERROR) << "poll error";
                break;
            case 0:
                LOG(LogLevel::INFO) << "poll time out...";
                break;
            default:
                LOG(LogLevel::DEBUG) << "有事件就绪了...";
                Dispatcher();
                break;
            }
        }
        Stop();
    }

    void Dispatcher()
    {
        for (int i = 0; i, size; i++)
        {
            if (_fds[i].fd == defaultfd)
                continue;
            if (_fds[i].revents & POLLIN)
            {
                if (_fds[i].fd == _listensock->Fd())
                {
                    Accepter();
                }
                else
                {
                    Recver(i);
                }
            }
        }
    }

    void Accepter()
    {
        InetAddr client;
        int fd = _listensock->Accept(&client);
        if (fd >= 0)
        {
            LOG(LogLevel::INFO) << "get a new link, sockfd: " << fd;
            int pos = 0;
            for (; pos < size; pos++)
            {
                if (_fds[pos].fd == defaultfd)
                    break;
            }

            if (pos == size)
            {
                LOG(LogLevel::WARNING) << "poll server full";
                close(fd);
            }
            else
            {
                _fds[pos].fd = fd;
                _fds[pos].events = POLLIN;
                _fds[pos].revents = 0;
            }
        }
    }

    void Recver(int pos)
    {
        char buffer[1024];
        ssize_t n = recv(_fds[pos].fd, buffer, sizeof(buffer) - 1, 0);
        if (n > 0)
        {
            buffer[n] = 0;
            std::cout << "client say@ " << buffer << std::endl;
        }
        else if (n == 0)
        {
            LOG(LogLevel::INFO) << "client quit...";
            close(_fds[pos].fd);
            _fds[pos].fd = defaultfd;
            _fds[pos].events = 0;
            _fds[pos].revents = 0;
        }
        else
        {
            LOG(LogLevel::ERROR) << "recv error";
            close(_fds[pos].fd);
            _fds[pos].fd = defaultfd;
            _fds[pos].events = 0;
            _fds[pos].revents = 0;
        }
    }

    void PrintFd()
    {
        std::cout << "_fds[]: ";
        for (int i = 0; i < size; i++)
        {
            if (_fds[i].fd == defaultfd)
                continue;
            std::cout << _fds[i].fd << " ";
        }
        std::cout << std::endl;
    }

    void Stop()
    {
        _isrunning = false;
    }

    ~PollServer()
    {
    }

private:
    std::unique_ptr<Socket> _listensock;
    bool _isrunning;
    struct pollfd _fds[size];
};